#include "main.h"

void SysClock_Init(void)
{
    SYS_ConfigHSI(SYS_CLK_HSI_64M);     /*设置内部高速时钟为64MHz*/
	SYS_EnableHSI();                    /*开启高速时钟*/
	SYS_ConfigAHBClock(SYS_CLK_SEL_HSI, SYS_CLK_DIV_1);     /*设置AHB时钟为高速时钟的1分频*/ 
	SYS_ConfigAPBClock(AHB_CLK_DIV_1);                      /*设置APB时钟为AHB时钟的1分频*/    
	SystemCoreClockUpdate();
}

void Powerup_Init(void)
{
    __disable_irq();    				//关总中断
    SysClock_Init();  
    SYS_DisableIOCFGProtect();			//关闭IOCONFIG写保护
	SYS_DisableGPIO0Protect();			//关闭GPIO0的相关寄存器写保护
	SYS_DisableGPIO1Protect();			//关闭GPIO1的相关寄存器写保护
	SYS_DisableGPIO2Protect();			//关闭GPIO2的相关寄存器写保护
	SYS_DisableGPIO3Protect();			//关闭GPIO3的相关寄存器写保护
	SYS_DisableGPIO4Protect();			//关闭GPIO4的相关寄存器写保护

   
	SVPWM_Init(20000);
	SC60228_Init();
	Timer1_Init(4000);
	Motor_Init();


	Massage_Init();
	M1_Init();
	M2_Init();
	
	Motor.PPN = POLE_NUMBER;
	
	//Motor.ThetaM_Offset = 134;
	
	Massage_ReadData();

	log_i("Motor.ThetaM = %u",Motor.ThetaM);
	log_i("Motor.ThetaE = %u",Motor.ThetaE);
	log_i("Motor.ThetaM_Offset = %u",Motor.ThetaM_Offset);
	
	
    __enable_irq();  					//开总中断

	
}

int main(void)
{
    Powerup_Init();

	while(1)
  {
		uart_comm_loop();
    if(TimeFlag_1ms)
    {
    	TimeFlag_1ms = 0;
			Massage_1ms();
			uart_comm_1ms();
		}
		Massage_System_Control();
		printLoop();

  }
}
